/**
 * 
 */
package ihm.views.main;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

import ihm.controllers.ATKcontroller;
import ihm.models.events.CharacterChangedEvent;
import ihm.models.events.EquipmentChangedEvent;
import ihm.models.events.ItemTypeChangedEvent;
import ihm.models.events.LanguageChangedEvent;
import ihm.models.events.QueryChangedEvent;
import ihm.models.events.StatChangedEvent;
import ihm.views.ATKview;
import ihm.views.main.catalog.CatalogPanel;

/**
 * The main frame of the software. As the main frame, this view extends the 
 * ATKview and,in extenso, implements the ATKlistener.
 * This view contains the following subview :
 * <ul>
 *  <li>The menu bar of the main view</li>
 *  <li>The items type tree</li>
 *  <li>The catalog of the equipable items</li>
 *  <li>The equipment panel</li>
 *  <li>The statistics panel</li>
 * </ul>
 * As the main view, the update resulting of different event are treated here
 * and each update is done by this class.
 * Moreover, all the subview are divide in different JSplitPane.
 * @author Baptiste Legrand & Benjamin Orman
 * @version 1.0
 */
public class MainFrame extends ATKview {

  private JFrame mainFrame;
  private MainMenuBar menuBar;
  private ItemsTree itemsTree;
  private CatalogPanel catalog;
  private EquipmentPanel equipmentPanel;
  private StatisticsPanel statPanel;
  private JSplitPane horizontalSplitPanel;
  private JSplitPane verticalLeftSplitPanel;
  private JSplitPane verticalRightSplitPanel;

  /**
   * Creates the Main Frame.
   * @param newController - The ATKcontroller required to notify changment.
   */
  public MainFrame(ATKcontroller newController)
  {
    super(newController);
    //Register this class to the controller.
    newController.getModel().addATKlistener(this);
    //Instanciate the JFrame
    this.mainFrame = new JFrame();
    this.mainFrame.setSize(900,800);
    //Set MenuBar
    this.menuBar = new MainMenuBar(newController, this.mainFrame);
    this.mainFrame.setJMenuBar(this.menuBar.getJMenuBar());
    this.catalog = new CatalogPanel(newController.getModel());
    this.buildSplitPanels();
    this.mainFrame.add(horizontalSplitPanel);
    //put the catalog(table) at the bottom
    this.horizontalSplitPanel.setBottomComponent(catalog);
    //put the vertical left panel (which will be contains the item tree, Equipment and Stats) at the top
    this.horizontalSplitPanel.setTopComponent(this.verticalLeftSplitPanel);
    //Create the itemTree view
    this.itemsTree = new ItemsTree(newController);
    //Create the EquipmentPanel
    this.equipmentPanel = new EquipmentPanel(newController);
    //Create the StatisticPanel
    this.statPanel = new StatisticsPanel(newController); 
    //
    this.verticalLeftSplitPanel.setLeftComponent(itemsTree.getPane());
    this.verticalLeftSplitPanel.setRightComponent(this.verticalRightSplitPanel);
    this.verticalRightSplitPanel.setLeftComponent(this.equipmentPanel);
    this.verticalRightSplitPanel.setRightComponent(this.statPanel);
  }

  private void buildSplitPanels()
  {
    this.horizontalSplitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    this.horizontalSplitPanel.setOneTouchExpandable(true);

    this.verticalLeftSplitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    this.verticalLeftSplitPanel.setOneTouchExpandable(true);
    this.verticalLeftSplitPanel.setBorder(BorderFactory.createEmptyBorder());

    this.verticalRightSplitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    this.verticalRightSplitPanel.setOneTouchExpandable(true);
    this.verticalRightSplitPanel.setBorder(BorderFactory.createEmptyBorder());

  }

  /**
   * @see ihm.views.ATKview#close()
   */
  @Override
  public void close() {
  }

  /**
   * @see ihm.views.ATKview#display()
   */
  @Override
  public void display() {
    //On positionne la frame sur l'ecran avant de l'afficher
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int height  = screenSize.height;
    int width   = screenSize.width;
    int x = (width - this.mainFrame.getWidth())/2;
    int y = (height - this.mainFrame.getHeight())/3; 
    this.mainFrame.setBounds(x, y, this.mainFrame.getWidth(), this.mainFrame.getHeight());
    this.mainFrame.setVisible(true);
  }

  /* (non-Javadoc)
   * @see ihm.models.ATKlistener#EquipmentChanged(ihm.models.events.EquipmentChangedEvent)
   */
  @Override
  public void EquipmentChanged(EquipmentChangedEvent event) {
    //Update the Equipment Panel
    this.equipmentPanel.EquipmentChanged(event);
    this.statPanel.EquipmentChanged(event);
  }

  /* (non-Javadoc)
   * @see ihm.models.ATKlistener#ItemTypeChanged(ihm.models.events.ItemTypeChangedEvent)
   */
  @Override
  public void ItemTypeChanged(ItemTypeChangedEvent event) {
    //Update the Catalog
    this.catalog.ItemTypeChanged(event);
  }

  /* (non-Javadoc)
   * @see ihm.models.ATKlistener#LanguageChanged(ihm.models.events.LanguageChangedEvent)
   */
  @Override
  public void LanguageChanged(LanguageChangedEvent event) {
    //Update the MainMenuBar
    this.menuBar.LanguageChanged(event);
    //Update the ItemTree Panel : To do that we just have to get the panel back
    this.verticalLeftSplitPanel.setLeftComponent(this.itemsTree.getPane());
    //Update the Equipment Panel
    this.equipmentPanel.LanguageChanged(event);
    //Update the Statistics Panel
    this.statPanel.LanguageChanged(event);
    //Update the Catalog
    this.catalog.LanguageChanged(event);
  }

  @Override
  public void QueryChanged(QueryChangedEvent event) {
    //Update the Catalog
    this.catalog.QueryChanged(event);
  }

  @Override
  public void StatChanged(StatChangedEvent event) {
    //Update the Equipment
    this.equipmentPanel.StatChanged(event);
    //Update the Statistics
    this.statPanel.StatChanged(event);
  }

  @Override
  public void CharacterChanged(CharacterChangedEvent event) {
    //Update the Equipment
    this.equipmentPanel.CharacterChanged(event);
    //Update the Statistics
    this.statPanel.CharacterChanged(event);
  }
}
